package application;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.text.StyledEditorKit.ForegroundAction;

import application.DisplayMap.Hero;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.AudioClip;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class MapDesigner extends Application {

	
	int blocksMap[][];
	ImageView[][] blockImageViews;
	Image[] originImages;
	
	int originLocation[];
	boolean isOriginSet;
	
	int numberOfDestinations;
	
	
	Image blankImage; 
	Image barrierImage; 

	Image destinationImage; 
	
	
	AudioClip errorAudio; 
	
	public MapDesigner() {
		this.blankImage = new Image("/pics/blank.png");	
		this.barrierImage = new Image("/pics/block.png");

		this.destinationImage = new Image("/pics/Destination.png");
		this.errorAudio =new AudioClip(new File("auds/Error.mp3").toURI().toString());
	

				
	}
	
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		
		StackPane stackPane = new StackPane();
		stackPane.setPadding(new Insets(20, 20, 20, 20));
		BorderPane basePane = new BorderPane();
		basePane.setLeft(getControlPane(stackPane, primaryStage));
		basePane.setCenter(stackPane);
		
		Scene scene = new Scene(basePane);
	
		primaryStage.setTitle("DesignMap");
		primaryStage.setScene(scene);
		primaryStage.show();
		//primaryStage.setResizable(false);
		stackPane.getChildren().clear();
		primaryStage.setWidth(1080);
		primaryStage.setHeight(720);
		primaryStage.centerOnScreen();
	}
	
	
	public VBox getControlPane(StackPane stackPane,Stage primaryStage) {
		VBox controlPane = new VBox();
		controlPane.setSpacing(20);
	
		
	
		TextField tfRows = new TextField();
		TextField tfColumns = new TextField();
		TextField tfLevelName = new TextField();
		
		Button saveButton = new Button("Save");
		saveButton.setOnMouseClicked(e ->{
			try {
			save(tfLevelName.getText());
			} catch (IOException e1) {
			e1.printStackTrace();
			}
		});

		
		
		tfRows.setPrefColumnCount(1);
		tfRows.setText("0");
		tfColumns.setPrefColumnCount(1);
		tfColumns.setText("0");

		tfLevelName.setPrefColumnCount(1);

		Button generateButton = new Button("Generate");
		
		GenerateHandler generateHandler = new GenerateHandler(tfRows.getText(), tfColumns.getText(), stackPane);
		
		tfRows.textProperty().addListener(ov -> generateHandler.newNumbersOfRows = Integer.parseInt(tfRows.getText()));
		tfColumns.textProperty().addListener(ov -> generateHandler.newNumberOfColumns = Integer.parseInt(tfColumns.getText()));
		
		generateButton.setOnMouseClicked(generateHandler);
		
		
		Button buttonAddRow = new Button("Add Row");
		buttonAddRow.setOnAction(new AddRowHandler(stackPane));
		Button buttonSubRow = new Button("Sub Row");
		buttonSubRow.setOnAction(new SubRowHandler(stackPane));
		
		Button buttonAddCol = new Button("Add Col");
		buttonAddCol.setOnAction(new AddColHandler(stackPane));
		Button buttonSubCol = new Button("Sub Col");
		buttonSubCol.setOnAction(new SubColHandler(stackPane));
		
		
		Button buttonRotate = new Button("Rotate");
		buttonRotate.setOnAction(e ->{
			if (isOriginSet) {
				if (blocksMap[originLocation[0]][originLocation[1]] < 4) {
					blocksMap[originLocation[0]][originLocation[1]] ++;
				}
				else if (blocksMap[originLocation[0]][originLocation[1]] == 4) {
					blocksMap[originLocation[0]][originLocation[1]] = 1;
				}
				blockImageViews[originLocation[0] + 1][originLocation[1] + 1].setImage(originImages[blocksMap[originLocation[0]][originLocation[1]] - 1]);
			}
		});
		Button exitButton = new Button("Exit");
		exitButton.setOnMouseClicked(e -> {
			primaryStage.close();
		});
		controlPane.getChildren().addAll(exitButton, new Label("Rows"), tfRows,
				 new Label ("Columns"), tfColumns, generateButton,buttonAddRow, buttonSubRow, buttonAddCol, buttonSubCol, buttonRotate,tfLevelName, saveButton);
		
		controlPane.setPadding(new Insets(20, 0, 0, 5));
		
		return controlPane;
	}
	
	class GenerateHandler implements EventHandler<MouseEvent>{
		int newNumbersOfRows;
		int newNumberOfColumns;
		StackPane stackPane;
	
		public GenerateHandler(String newNumberOfRows, String newNumberOfColumns, StackPane stackPane) {
			this.newNumberOfColumns = Integer.parseInt(newNumberOfColumns);
			this.newNumbersOfRows = Integer.parseInt(newNumberOfRows);
			this.stackPane = stackPane;
		}
		
		@Override
		public void handle(MouseEvent e) {
			stackPane.getChildren().clear();
			stackPane.getChildren().add(getMapPane(newNumbersOfRows, newNumberOfColumns));
		}
	}
	
	
	
	public Pane getMapPane(int numberOfRows, int numberOfColumns) {
		isOriginSet = false;
		numberOfDestinations = 0;
		
		blocksMap = new int[numberOfRows][numberOfColumns];
		
		for (int[] m: blocksMap) {
			for (int n: m) {
				n = 0;
			}
		}
		
		return getMapPane(blocksMap);
	}
	
	
	public Pane getMapPane(int[][] blocksMap) {
		
	
		
		int numberOfRows = blocksMap.length;
		int numberOfColumns = blocksMap[0].length;
		
		blockImageViews = new ImageView[numberOfRows + 2][numberOfColumns + 2];
		
		Pane mapPane = new Pane();		
		Image upColumnImage = new Image("/pics/UpColumn.png");	
		Image downColumnImage = new Image("/pics/DownColumn.png");
		Image leftRowImage = new Image("/pics/LeftRow.png");
		Image rightRowImage = new Image("/pics/RightRow.png");
		Image allImage = new Image("/pics/All.png");
		ImageView all = new ImageView(allImage);
		
		all.setFitWidth(32);
		all.setFitHeight(32);
		all.setX(0);
		all.setY(0);
		
		for (int i = 0; i < blockImageViews.length; i++) {
			for (int j = 0; j < blockImageViews[i].length; j++) {
				if (i == 0 ||j == 0 || 
						i == blockImageViews.length - 1 || j == blockImageViews[i].length - 1 ){
					blockImageViews[i][j] = new ImageView(barrierImage);
					blockImageViews[i][j].setFitWidth(32);
					blockImageViews[i][j].setFitHeight(32);
					mapPane.getChildren().add(blockImageViews[i][j]);
					blockImageViews[i][j].setX(32 * j);
					blockImageViews[i][j].setY(32 * i);
				}
				
				if (j == 0 && i != j && i < blockImageViews.length - 1) {
					blockImageViews[i][j] = new ImageView(rightRowImage);
					blockImageViews[i][j].setFitWidth(32);
					blockImageViews[i][j].setFitHeight(32);
					mapPane.getChildren().add(blockImageViews[i][j]);
					blockImageViews[i][j].setX(32 * j);
					blockImageViews[i][j].setY(32 * i);
					blockImageViews[i][j].setOnMouseClicked(new RowHandler(i));
				}
				
				if (j == blockImageViews[i].length - 1 && i < blockImageViews.length - 1  && i > 0) {
					blockImageViews[i][j] = new ImageView(leftRowImage);
					blockImageViews[i][j].setFitWidth(32);
					blockImageViews[i][j].setFitHeight(32);
					mapPane.getChildren().add(blockImageViews[i][j]);
					blockImageViews[i][j].setX(32 * j);
					blockImageViews[i][j].setY(32 * i);
					blockImageViews[i][j].setOnMouseClicked(new RowHandler(i));
				}
				
				if (i == 0 && j != i && j < blockImageViews[i].length - 1) {
					blockImageViews[i][j]  = new ImageView(downColumnImage);
					blockImageViews[i][j].setFitHeight(32);
					blockImageViews[i][j].setFitWidth(32);
					mapPane.getChildren().add(blockImageViews[i][j]);
					blockImageViews[i][j].setX(32 * j);
					blockImageViews[i][j].setY(32 * i);
					blockImageViews[i][j].setOnMouseClicked(new ColumnHandler(j));
				}
				
				if (i == blockImageViews.length - 1  && j < blockImageViews[i].length - 1 && j > 0) {
					blockImageViews[i][j]  = new ImageView(upColumnImage);
					blockImageViews[i][j].setFitHeight(32);
					blockImageViews[i][j].setFitWidth(32);
					mapPane.getChildren().add(blockImageViews[i][j]);
					blockImageViews[i][j].setX(32 * j);
					blockImageViews[i][j].setY(32 * i);
					blockImageViews[i][j].setOnMouseClicked(new ColumnHandler(j));
				}
				
				
				if (i > 0 && i < blockImageViews.length - 1 && j > 0 && j < blockImageViews[i].length - 1) {
					if (blocksMap[i-1][j-1] == 0) {
						blockImageViews[i][j] = new ImageView(blankImage);
						blockImageViews[i][j].setFitWidth(32);
						blockImageViews[i][j].setFitHeight(32);
						mapPane.getChildren().add(blockImageViews[i][j]);
						blockImageViews[i][j].setX(32 * j );
						blockImageViews[i][j].setY(32 * i );
						blockImageViews[i][j].setOnMouseClicked(new PlaceHandler(i - 1, j - 1));
					}
					else if(blocksMap[i-1][j-1] == -1) {
						blockImageViews[i][j] = new ImageView(barrierImage);
						blockImageViews[i][j].setFitWidth(32);
						blockImageViews[i][j].setFitHeight(32);
						mapPane.getChildren().add(blockImageViews[i][j]);
						blockImageViews[i][j].setX(32 * j );
						blockImageViews[i][j].setY(32 * i );
						blockImageViews[i][j].setOnMouseClicked(new LiftHandler(i - 1, j - 1));
					}
					else if(blocksMap[i-1][j-1] > 0 && blocksMap[i-1][j-1] < 5) {
						blockImageViews[i][j] = new ImageView(originImages[blocksMap[i-1][j-1] - 1]);
						blockImageViews[i][j].setFitWidth(32);
						blockImageViews[i][j].setFitHeight(32);
						mapPane.getChildren().add(blockImageViews[i][j]);
						blockImageViews[i][j].setX(32 * j );
						blockImageViews[i][j].setY(32 * i );
						blockImageViews[i][j].setOnMouseClicked(new CancelOriginHandler(i - 1, j - 1));
					}
					else if (blocksMap[i-1][j-1] == -99) {
						blockImageViews[i][j] = new ImageView(destinationImage);
						blockImageViews[i][j].setFitWidth(32);
						blockImageViews[i][j].setFitHeight(32);
						mapPane.getChildren().add(blockImageViews[i][j]);
						blockImageViews[i][j].setX(32 * j );
						blockImageViews[i][j].setY(32 * i );
						blockImageViews[i][j].setOnMouseClicked(new CancelDestinationHandler(i - 1, j - 1));
					}
					
				}
				
			}
		}
		
		mapPane.getChildren().add(all);
		all.setOnMouseClicked(new AllHandler());
		return mapPane;
	}
	
	
	public void placeBlock(int i, int j) {
		blocksMap[i][j] = -1;
		blockImageViews[i + 1][j + 1].setImage(barrierImage);
		blockImageViews[i + 1][j + 1].setFitWidth(32);
		blockImageViews[i + 1][j + 1].setFitHeight(32);
		blockImageViews[i + 1][j + 1].setOnMouseClicked(new LiftHandler(i, j));
	}

	public void liftBlock(int i, int j) {
		blocksMap[i][j] = 0;
		blockImageViews[i + 1][j + 1].setImage(blankImage);
		blockImageViews[i + 1][j + 1].setFitWidth(32);
		blockImageViews[i + 1][j + 1].setFitHeight(32);
		blockImageViews[i + 1][j + 1].setOnMouseClicked(new PlaceHandler(i, j));
		blockImageViews[i + 1][j + 1].setRotate(0);
	}

	public void setDestination(int i, int j) {
		blocksMap[i][j] = -99;
		blockImageViews[i + 1][j + 1].setImage(destinationImage);
		blockImageViews[i + 1][j + 1].setFitWidth(32);
		blockImageViews[i + 1][j + 1].setFitHeight(32);
		blockImageViews[i + 1][j + 1].setOnMouseClicked(new CancelDestinationHandler(i, j));
		numberOfDestinations++;
	}
	
	public void setOrigin(int i, int j) {
		originImages = new Image[4];
		originImages[0] = new Image("/pics/Origin_Down.png");
		originImages[1] = new Image("/pics/Origin_Left.png");
		originImages[2] = new Image("/pics/Origin_Up.png");
		originImages[3] = new Image("/pics/Origin_Right.png");
		
		if (!isOriginSet) {
			blocksMap[i][j] = 1;
			blockImageViews[i + 1][j + 1].setImage(originImages[0]);
			blockImageViews[i + 1][j + 1].setFitWidth(32);
			blockImageViews[i + 1][j + 1].setFitHeight(32);
			blockImageViews[i + 1][j + 1].setOnMouseClicked(new CancelOriginHandler(i, j));

			originLocation = new int[2];
			originLocation[0] = i;
			originLocation[1] = j;
			isOriginSet = true;
		}
	}
	
	
	class CancelDestinationHandler implements EventHandler<MouseEvent>{
		int i;
		int j;
		
		public CancelDestinationHandler(int i, int j){
			this.i = i;
			this.j = j;
		}
		
		@Override
		public void handle(MouseEvent e) {
			if (e.getButton() == MouseButton.SECONDARY) {
				liftBlock(i, j);
				numberOfDestinations--;
			}
		}
	}
	
	class CancelOriginHandler implements EventHandler<MouseEvent>{
		int i;
		int j;
		
		public CancelOriginHandler(int i, int j) {
			this.i = i;
			this.j = j;
		}
		
		@Override
		public void handle(MouseEvent e) {
			// TODO Auto-generated method stub
			if (e.getButton() == MouseButton.MIDDLE) {
				liftBlock(i, j);
				isOriginSet = false;
				originLocation = new int[2];
			}
		}
	}
	
	

	class PlaceHandler implements EventHandler<MouseEvent>{
		int i;
		int j;
		
		public PlaceHandler(int i, int j){
			this.i = i;
			this.j = j;
		}
	
		@Override
		public void handle(MouseEvent e) {
			if (e.getButton() == MouseButton.PRIMARY) {
				placeBlock(i, j);
			}
			else if (e.getButton() == MouseButton.SECONDARY) {
				setDestination(i, j);
			}
			else if (e.getButton() == MouseButton.MIDDLE) {
				setOrigin(i, j);
			}
		}	
	}

	class LiftHandler implements EventHandler<MouseEvent>{
		int i;
		int j;
		public LiftHandler(int i, int j){
			this.i = i;
			this.j = j;
		}
		@Override
		public void handle(MouseEvent e) {
			// TODO Auto-generated method stub
			if (e.getButton() == MouseButton.PRIMARY) {
				liftBlock(i, j);
			}
		}
		
	}
	
	
	class RowHandler implements EventHandler<MouseEvent>{
		int i = 0;
		
		public RowHandler(int i) {
			this.i = i;
		}
		
		@Override
		public void handle(MouseEvent e) {
			for (int j = 1; j < blocksMap[0].length + 1; j++) {
				if (blocksMap[i - 1][j - 1] == 0) {placeBlock(i - 1, j - 1);}
				else if (blocksMap[i - 1][j - 1] == -1) {liftBlock(i - 1, j - 1);}
			}
		}
	}
	
	
	class ColumnHandler implements EventHandler<MouseEvent>{
		int j;
		public ColumnHandler(int j) {
			this.j = j;
		}
		
		@Override
		public void handle(MouseEvent e) {
			for (int i = 1; i < blocksMap.length + 1; i++) {
				if (blocksMap[i - 1][j - 1] == 0) {placeBlock(i - 1, j - 1);}
				else if (blocksMap[i - 1][j - 1] == -1) {liftBlock(i - 1, j - 1);}
			}	
		}
	}
	

	class AllHandler implements EventHandler<MouseEvent>{
		@Override
		public void handle(MouseEvent e) {
			for (int i = 1; i < blocksMap.length + 1; i++) {
				for (int j = 1; j < blocksMap[0].length + 1; j++) {
					if (blocksMap[i - 1][j - 1] == 0) {placeBlock(i - 1, j - 1);}
					else if (blocksMap[i - 1][j - 1] == -1) {liftBlock(i - 1, j - 1);}
				}
			}
		}
	}
	
	
	
	public void save(String filename) throws IOException{
		
			if (isOriginSet && numberOfDestinations > 0) {
				if (filename.equals("")) {
					BasicTools.launchStage(new NameNoSet());
					errorAudio.play();
				}
				else if (filename.length() > 0){
					boolean isWithoutSpacing = true;
					
					for (int i = 0; i < filename.length(); i++) {
						if (filename.charAt(i) == ' ')
							isWithoutSpacing = false;
					}
					if (! isWithoutSpacing) {
						BasicTools.launchStage(new NameWithSpacing());
						errorAudio.play();
					}
					else {
						File file = new File("gamelevels/free_mode_levels/" + filename + ".txt");
						
						if (!file.exists()) {
							
							try (PrintWriter listPrintWriter = new PrintWriter(new FileWriter("gamelevels/free_mode_levels/list.txt", true))) {
								listPrintWriter.println(filename);
							}
							
							try (PrintWriter printWriter = new PrintWriter(file)){
								for (int i = 0; i < blocksMap.length ; i++) {
									for (int j = 0; j < blocksMap[i].length ; j++) {
										if (j == blocksMap[i].length - 1) {printWriter.println(blocksMap[i][j] + "");}
										else {printWriter.print(blocksMap[i][j] + " ");}	
									}
								}
							}
						}
						else {
							BasicTools.launchStage(new ReplaceOrNot(filename));
							errorAudio.play();
						}
					}
				}
			}
			else {
				BasicTools.launchStage(new ElementsNoSet());
				errorAudio.play();
			}
	}
	
	

	class AddRowHandler implements EventHandler<ActionEvent>{
		StackPane stackPane;
		public AddRowHandler(StackPane stackPane) {
			this.stackPane = stackPane;
		}
		@Override
		public void handle(ActionEvent e) {
			
			int[][]  newBlocksMap = new int[blocksMap.length + 1][blocksMap[0].length];
			for (int i = 0; i < newBlocksMap.length; i++) {
				for (int j = 0; j < newBlocksMap[0].length; j++) {
					if (i < blocksMap.length && j < blocksMap[0].length) {
						newBlocksMap[i][j] = blocksMap[i][j];
					}
					else {
						newBlocksMap[i][j] = 0;
					}
				}
			}
			stackPane.getChildren().clear();
			stackPane.getChildren().add(getMapPane(newBlocksMap));
			blocksMap = newBlocksMap;
		}
	}
	
	
	class SubRowHandler implements EventHandler<ActionEvent>{
		
		StackPane stackPane;
		public SubRowHandler(StackPane stackPane) {
			this.stackPane = stackPane;
		}
		
		@Override
		public void handle(ActionEvent e) {
			if (blocksMap.length > 1) {
				
				int[] rowSubbed = blocksMap[blocksMap.length - 1];
				int numberOfDestinationSet = 0;
				
				for (int i: rowSubbed) {
					if (i > 0 && i < 5) {
						isOriginSet = false;
					}
					else if (i == -99) {
						numberOfDestinationSet++;
					}
				}
				numberOfDestinations -= numberOfDestinationSet;  
				

				
				int[][]  newBlocksMap = new int[blocksMap.length - 1][blocksMap[0].length];
				for (int i = 0; i < newBlocksMap.length; i++) {
					for (int j = 0; j < newBlocksMap[0].length; j++) {
						if (i < blocksMap.length && j < blocksMap[0].length) {
							newBlocksMap[i][j] = blocksMap[i][j];
						}
						else {
							newBlocksMap[i][j] = 0;
						}
					}
				}
				stackPane.getChildren().clear();
				stackPane.getChildren().add(getMapPane(newBlocksMap));
				blocksMap = newBlocksMap;
		
			}
		}
	}
	
	
	class AddColHandler implements EventHandler<ActionEvent>{
		StackPane stackPane;
		public AddColHandler(StackPane stackPane) {
			this.stackPane = stackPane;
		}
		@Override
		public void handle(ActionEvent e) {
			
			int[][]  newBlocksMap = new int[blocksMap.length][blocksMap[0].length + 1];
			for (int i = 0; i < newBlocksMap.length; i++) {
				for (int j = 0; j < newBlocksMap[0].length; j++) {
					if (i < blocksMap.length && j < blocksMap[0].length) {
						newBlocksMap[i][j] = blocksMap[i][j];
					}
					else {
						newBlocksMap[i][j] = 0;
					}
				}
			}
			stackPane.getChildren().clear();
			stackPane.getChildren().add(getMapPane(newBlocksMap));
			blocksMap = newBlocksMap;
		}
	}
	
	class SubColHandler implements EventHandler<ActionEvent>{
		StackPane stackPane;
		public SubColHandler(StackPane stackPane) {
			this.stackPane = stackPane;
		}
		@Override
		public void handle(ActionEvent e) {
			if (blocksMap[0].length > 1) {
				
		
				int numberOfDestinationSet = 0;
				int jj = blocksMap[0].length - 1;
				
				for (int i = 0; i < blocksMap.length; i++) {
					if (blocksMap[i][jj] > 0 && blocksMap[i][jj] < 5) {
						isOriginSet = false;
					}
					else if (blocksMap[i][jj] == -99) {
						numberOfDestinationSet++;
					}
				}
				numberOfDestinations -= numberOfDestinationSet; 
				
			
				
				int[][]  newBlocksMap = new int[blocksMap.length][blocksMap[0].length - 1];
				for (int i = 0; i < newBlocksMap.length; i++) {
					for (int j = 0; j < newBlocksMap[0].length; j++) {
						if (i < blocksMap.length && j < blocksMap[0].length) {
							newBlocksMap[i][j] = blocksMap[i][j];
						}
						else {
							newBlocksMap[i][j] = 0;
						}
					}
				}
				stackPane.getChildren().clear();
				stackPane.getChildren().add(getMapPane(newBlocksMap));
				blocksMap = newBlocksMap;
			}
		}
	}
	
	
	
	class ReplaceOrNot extends Application{
		
		String levelName;
		
		public ReplaceOrNot(String levelName) {
			this.levelName = levelName;
		}
		
		@Override
		public void start(Stage primaryStage) throws Exception {
			// TODO Auto-generated method stub
			
			Pane pane = getPane(primaryStage);
			Scene scene = new Scene(pane);
			primaryStage.setScene(scene);
			primaryStage.show();
			primaryStage.centerOnScreen();
			primaryStage.setResizable(false);
		}
		
		
		public BorderPane getPane(Stage primaryStage) {
			Label label1 = new Label("level " + levelName + " has already existed!");
			label1.setFont(new Font(15));
			Label label2 = new Label("Replace it or not?");
			label2.setFont(new Font(15));
			VBox vBox = new VBox(5);
			vBox.setAlignment(Pos.CENTER);
			vBox.getChildren().addAll(label1,label2);
			
			
			Button buttonYes = new Button("Yes");
			buttonYes.setFont(new Font(15));
			buttonYes.setOnAction(new YesHandler(primaryStage, levelName));
			
			Button buttonNo = new Button("No");
			buttonNo.setFont(new Font(15));
			buttonNo.setOnAction(new NoHandler(primaryStage));
			
			HBox hBox = new HBox(30);
			hBox.setAlignment(Pos.CENTER);
			hBox.setPadding(new Insets(5, 0, 0, 0));
			hBox.getChildren().addAll(buttonYes, buttonNo);
			
			vBox.getChildren().add(hBox);
			//vBox.setStyle("-fx-background-color: grey");
			
			BorderPane pane = new BorderPane();
		
			pane.setPadding(new Insets(10, 10, 10, 10));
			pane.setCenter(vBox);
			
			return pane;
		}
				
		public class YesHandler implements EventHandler<ActionEvent>{
			
			Stage primaryStage;
			String levelName;
			public YesHandler(Stage primaryStage, String levelname) {
				// TODO Auto-generated constructor stub
				this.primaryStage = primaryStage;
				this.levelName = levelname;
			}
			
			@Override
			public void handle(ActionEvent e) {
				File file = new File("gamelevels/free_mode_levels/" + levelName + ".txt");
				try (PrintWriter printWriter = new PrintWriter(file)){
					for (int i = 0; i < blocksMap.length ; i++) {
						for (int j = 0; j < blocksMap[i].length ; j++) {
							if (j == blocksMap[i].length - 1) {printWriter.println(blocksMap[i][j] + "");}
							else {printWriter.print(blocksMap[i][j] + " ");}	
						}
					}
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				primaryStage.close();
			}
		}
		
		
		
		public class NoHandler implements EventHandler<ActionEvent>{
			Stage primaryStage;
			
			public NoHandler(Stage primaryStage) {
				this.primaryStage = primaryStage;
			}
			
			@Override
			public void handle(ActionEvent arg0) {
				// TODO Auto-generated method stub
				primaryStage.close();
			}
		}
	}
	
	
	class ElementsNoSet extends Application{
		@Override
		public void start(Stage primaryStage) throws Exception {
			// TODO Auto-generated method stub
			
			Pane pane = getPane(primaryStage);
			Scene scene = new Scene(pane);
			primaryStage.setScene(scene);
			primaryStage.show();
			primaryStage.centerOnScreen();
			primaryStage.setResizable(false);
		}
		
		
		public BorderPane getPane(Stage primaryStage) {
			Label label1 = new Label("No Origin or Destination!");
			label1.setFont(new Font(15));
			Label label2 = new Label(" ");
			label2.setFont(new Font(15));
			VBox vBox = new VBox(5);
			vBox.setAlignment(Pos.CENTER);
			vBox.getChildren().addAll(label1,label2);
			
			
			Button buttonYes = new Button("Yes");
			buttonYes.setFont(new Font(15));
			buttonYes.setOnAction(e -> primaryStage.close());
			
;
			
			HBox hBox = new HBox(30);
			hBox.setAlignment(Pos.CENTER);
			hBox.setPadding(new Insets(5, 0, 0, 0));
			hBox.getChildren().addAll(buttonYes);
			
			vBox.getChildren().add(hBox);
			//vBox.setStyle("-fx-background-color: grey");
			
			BorderPane pane = new BorderPane();
		
			pane.setPadding(new Insets(10, 10, 10, 10));
			pane.setCenter(vBox);
			
			return pane;
		}
				
		public class YesHandler implements EventHandler<ActionEvent>{
			
			Stage primaryStage;
	
			public YesHandler(Stage primaryStage) {
				// TODO Auto-generated constructor stub
				this.primaryStage = primaryStage;
			}
			
			@Override
			public void handle(ActionEvent e) {
				primaryStage.close();
			}
		}		
	}
	
	class NameNoSet extends Application{
		@Override
		public void start(Stage primaryStage) throws Exception {
			// TODO Auto-generated method stub
			
			Pane pane = getPane(primaryStage);
			Scene scene = new Scene(pane);
			primaryStage.setScene(scene);
			primaryStage.show();
			primaryStage.centerOnScreen();
			primaryStage.setResizable(false);
		}
		
		
		public BorderPane getPane(Stage primaryStage) {
			Label label1 = new Label("Without Level Name!");
			label1.setFont(new Font(15));
			Label label2 = new Label(" ");
			label2.setFont(new Font(15));
			VBox vBox = new VBox(5);
			vBox.setAlignment(Pos.CENTER);
			vBox.getChildren().addAll(label1,label2);
			
			
			Button buttonYes = new Button("Yes");
			buttonYes.setFont(new Font(15));
			buttonYes.setOnAction(e -> primaryStage.close());
			HBox hBox = new HBox(30);
			hBox.setAlignment(Pos.CENTER);
			hBox.setPadding(new Insets(5, 0, 0, 0));
			hBox.getChildren().addAll(buttonYes);
			
			vBox.getChildren().add(hBox);
			//vBox.setStyle("-fx-background-color: grey");
			
			BorderPane pane = new BorderPane();
		
			pane.setPadding(new Insets(10, 10, 10, 10));
			pane.setCenter(vBox);
			
			return pane;
		}
	}
	
	
	
	class NameWithSpacing extends Application{
		@Override
		public void start(Stage primaryStage) throws Exception {
			// TODO Auto-generated method stub
			
			Pane pane = getPane(primaryStage);
			Scene scene = new Scene(pane);
			primaryStage.setScene(scene);
			primaryStage.show();
			primaryStage.centerOnScreen();
			primaryStage.setResizable(false);
		}
		
		
		public BorderPane getPane(Stage primaryStage) {
			Label label1 = new Label("Name should be without spacing!");
			label1.setFont(new Font(15));
			Label label2 = new Label(" ");
			label2.setFont(new Font(15));
			VBox vBox = new VBox(5);
			vBox.setAlignment(Pos.CENTER);
			vBox.getChildren().addAll(label1,label2);
			
			
			Button buttonYes = new Button("Yes");
			buttonYes.setFont(new Font(15));
			buttonYes.setOnAction(e -> primaryStage.close());
			HBox hBox = new HBox(30);
			hBox.setAlignment(Pos.CENTER);
			hBox.setPadding(new Insets(5, 0, 0, 0));
			hBox.getChildren().addAll(buttonYes);
			
			vBox.getChildren().add(hBox);
			//vBox.setStyle("-fx-background-color: grey");
			
			BorderPane pane = new BorderPane();

			pane.setPadding(new Insets(10, 10, 10, 10));
			pane.setCenter(vBox);
			
			return pane;
		}	
	}
	
	
	public static void main(String[] args) {
			Application.launch(args);
	}
	
}
